$(document).ready(function()
    {
        $.notif_angsuran = function(data)
        {
            var total_pinjaman = 0, total_piutang = 0, total_tagihan = 0, sign = 0;
            $("#LIST_POTONGAN tbody").html('');
            for(var i = 0, j = 0; i < data.data.info_usaha.length; i++)
            {
                sign = 0;
                for(; j < data.data.pinjaman.length; j++)
                {
                    var current = data.data.pinjaman[j];
                    if(current.ID_INFO_USAHA != data.data.info_usaha[i].KODE_USAHA)
                        break;
                    var num = (sign == 0) ? (i + 1) : " ";
                    $("#LIST_POTONGAN tbody").append("<tr>\n\
                                    <td>"+num+"</td>\n\
                                    <td>" + current.URAIAN_USAHA + "</td>\n\
                                    <td>" + current.TGL_PINJAMAN + "</td>\n\
                                    <td>" + current.LAMA_PINJAMAN + "</td>\n\
                                    <td>" + current.BUNGA + "</td>\n\
                                    <td>" + current.JUMLAH_PINJAMAN + "</td>\n\
                                    <td>" + (current.JUMLAH_PINJAMAN - current.PIUTANG) + "</td>\n\
                                    <td>" + current.POTONGAN + "</td>\n\
                                </tr>");
                    sign = 1;
                    total_pinjaman += current.JUMLAH_PINJAMAN * 1;
                    total_piutang += (current.JUMLAH_PINJAMAN - current.PIUTANG) * 1;
                    total_tagihan += current.POTONGAN * 1;
                }
                if(sign == 0)
                    $("#LIST_POTONGAN tbody").append("<tr>\n\
                                    <td>" + (i + 1) + "</td>\n\
                                    <td>" + data.data.info_usaha[i].URAIAN_USAHA + "</td>\n\
                                    <td>-</td>\n\
                                    <td>-</td>\n\
                                    <td>-</td>\n\
                                    <td>-</td>\n\
                                    <td>-</td>\n\
                                    <td>-</td>\n\
                                </tr>");
            }
            $("#LIST_POTONGAN tfoot").html("<tr>\n\
                                        <th colspan='5'>Total</th>\n\
                                        <th>"+total_pinjaman+"</th>\n\
                                        <th>"+total_piutang+"</th>\n\
                                        <th>"+total_tagihan+"</th>\n\
                                        <th></th>\n\
                                        </tr>");
        }

        $("#FORM_ANGSURAN").submit(function(){
            return false;
        });


        $("#Cari").click(function()
        {
            $.data_ajax('br-03/angsuran/anggota/', "#FORM_ANGSURAN", $.notif_angsuran);
        });

        $.notif_simpan = function(data)
        {
            window.location = base_url + "br-03/angsuran/edit";
        }

        $("#Cetak").click(function()
        {
            $.data_ajax('br-08/potongan/cetak_rencana/'+$("#ID_ANGGOTA").val(), "#FORM_ANGSURAN", $.notif_simpan);
        });
    });